<?php
/**
 * 友情链接
 * User: hgq
 * Date: 2016/7/8 0008
 * Time: 上午 10:56
 */
use yii\helpers\Url;

?>
<script type="application/javascript">

    $(function ($) {
        //设置机构是否可选择
        var currentManager = <?=json_encode(\app\models\Manager::getCurrentManager()) ?>;
        var current_institution_code = currentManager.institution_code;
        if (current_institution_code != null && current_institution_code != '' && current_institution_code != '0') {
            $("#institution").attr("disabled", "disabled");
        }
    });

    //筛选
    function filter() {

        var institution_code = $("#institution").val();
        var name = $("#head_name").val();
        var is_shown = $("#head_is_shown").val();

        var href = window.location.href;
        href = setUrlParam(href, 'page', 1);


        if (institution_code != '' && institution_code != null) {
            href = setUrlParam(href, 'inst', institution_code);
        } else {
            $("#hint_message").text('请选择机构！');
            $("#hintMessageModal").modal('show');
            return;
        }
        if (name != '' && name != null) {
            href_str = href_str + "&name=" + name;
            href = setUrlParam(href, 'name', name);
        }
        if (is_shown != '' && is_shown != null) {
            href_str = href_str + "&is_shown=" + is_shown;
            href = setUrlParam(href, 'is_shown', is_shown);
        }

        window.location.href = href;
    }

    function refresh() {
        var href = decodeURIComponent(window.location.href);
        window.location.href = href;
    }

    //查看详情
    function showData(id, institution_code) {
        var data = {};
        data.id = id;
        data.institution_code = institution_code;

        $("#myModalShowLabelInst").html('');
        $("#modalshow").html('');
        requestHtml('friendly-link-part-view', data, true, function (responseHtml) {
            $("#modalshow").html(responseHtml);
        });

        $("#myModalShowLabelInst").html(institution_code);
    }

    //继续新增
    function continueAddData() {
        $("#edit_id").val('');
        $("#edit_name").val('');
        $("#edit_category_id").val('');
        $("#edit_link_url").val('');
        $("#edit_is_shown").val('1');
    }

    function onAddData() {
        var institution_code = $("#institution").val();
        if (institution_code == '' || institution_code == null) {
            $("#hint_message").text('请选择机构！');
            $("#hintMessageModal").modal('show');
            return;
        }
        addData();
    }

    //新增
    function addData() {
        $("#myModalEdit").modal('show');
        $("#myModalEditLabelInst").html('');
        var institution_code = $("#institution").val();
        var data = {};
        data.institution_code = institution_code;
        modaleAdEdit(data);
        $("#continueAdd").css("display", "block");
        $("#myModalEditLabelInst").html(institution_code);
    }

    //修改
    function editData(id, institution_code) {
        $("#myModalEditLabelInst").html('');
        var data = {};
        data.institution_code = institution_code;
        data.id = id;
        modaleAdEdit(data);
        $("#continueAdd").css("display", "none");
        $("#myModalEditLabelInst").html(institution_code);
    }

    //打开新增修改页面
    function modaleAdEdit(data) {
        $("#modaledit").html('');
        requestHtml('friendly-link-part-view', data, true, function (responseHtml) {
            $("#modaledit").html(responseHtml);
        });
    }

    //数据保存
    function saveData() {
        var id = $("#edit_id").val();
        var institution_code = $("#edit_institution_code").val();
        var name = $("#edit_name").val();
        var category_id = $("#edit_category_id").val();
        var link_url = $("#edit_link_url").val();
        var is_shown = $("#edit_is_shown").val();

        if (name == null || name == '') {
            $("#hint_message").text('广告名称不能为空！');
            $("#hintMessageModal").modal('show');
        } else if (category_id == null || category_id == '') {
            $("#hint_message").text('所属分类不能为空！');
            $("#hintMessageModal").modal('show');
        } else {
            var data = {};
            data.id = id;
            data.institution_code = institution_code;
            data.name = name;
            data.category_id = category_id;
            data.link_url = link_url;
            data.is_shown = is_shown;
            alert(id);
            requestJson("friendly-link-save-handler", data, true, function (response) {
                if (response.status == 0) {
                    var rtid = response.data.id;
                    $("#edit_id").val(rtid);
                    $("#hint_message").text('操作成功！');
                } else {
                    $("#hint_message").text('操作失败！');
                }
                $("#hintMessageModal").modal('show');
            });
        }
    }
    //删除数据
    function delData() {
        var id = $("#deleteDataId").val();
        delClose();
        if (id != null && id != '') {
            var data = {};
            data.id = id;
            requestJson("friendly-link-delete-handler", data, true, function (response) {
                if (response.status == 0) {
                    $("#hint_message").text('操作成功！');
                    refresh();
                } else {
                    $("#hint_message").text('操作失败！');
                }
                $("#hintMessageModal").modal('show');
            });
        } else {
            $("#hint_message").text('需删除的数据有误，请重试！');
            $("#hintMessageModal").modal('show');
        }

    }

    //删除取消
    function delClose() {
        $("#deleteDataId").val('');
        $("#deleteModal").modal('hide');
    }

    //删除按键点击事件
    function delClick(id) {
        $("#deleteDataId").val(id);
    }

    function categoryManage() {
        var institution_code = $("#institution").val();
        if (institution_code == '' || institution_code == null) {
            $("#hint_message").text('请选择机构！');
            $("#hintMessageModal").modal('show');
            return;
        }
        window.location.href = "friendly-link-category?inst=" + institution_code;
        loading.show();
    }

    //设置模态窗 1.当用户点击模态框外部时不会关闭 2.当按下 escape 键时不会关闭 3.当初始化时不显示模态框。
    $(function () {
        $('#myModalEdit').modal({
            keyboard: false, backdrop: 'static', show: false
        });
        $('#deleteModal').modal({
            keyboard: false, backdrop: 'static', show: false
        });
    });

</script>
<style type="text/css">
    .modal-body {
        overflow: auto;
        max-height: 600px;
    }
</style>
<!-- head filter-->
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group">
<!--                <button class="btn btn-primary" type="button" onclick="categoryManage()">-->
<!--                    分类管理-->
<!--                </button>-->
                <button class="btn btn-primary" type="button" onclick="onAddData()"><i class="fa fa-plus"></i> 新增
                </button>
            </div>
            <div class="pull-right">
                <div class="form-group">
                    <label class="sr-only" for="exampleInputPassword2">机构</label>
                    <?= app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'institution', 'currentInstitutionCode' => $institution_code, 'defaultText' => '——选择机构——']) ?>
                </div>
                <label class="sr-only" for="exampleInputPassword2">友情链接名称</label>
                <input type="text" class="form-control" id="head_name" placeholder="友情链接名称" value="<?= $name ?>">
                <select id="head_is_shown" class="form-control" onchange="">
                    <option value="" <?php if ($is_shown == "-1") echo "selected=\"selected\""; ?>>是否显示</option>
                    <option value="1" <?php if ($is_shown == "1") echo "selected=\"selected\""; ?>>显示</option>
                    <option value="0" <?php if ($is_shown == "0") echo "selected=\"selected\""; ?>>不显示</option>
                </select>
                <button type="button" class="btn btn-primary" onclick="filter()"><i class="fa fa-search"></i> 搜索
                </button>
            </div>
        </div>
    </div>
</div>
<!-- body table-->
<div class="panel panel-default" id="goodsList">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th style="width:35%">友情链接名称</th>
                    <th style="width:25%">所属分类</th>
                    <th style="width:10%">是否显示</th>
                    <th style="width:15%">创建时间</th>
                    <th style="width:15%">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                if ($list != null && $list != '') {
                    for ($i = 0; $i < count($list); $i++) {
                        ?>
                        <tr>
                            <td><a href="#"
                                   onclick="showData('<?= $list[$i]['id'] ?>','<?= $list[$i]['institution_code'] ?>')"
                                   data-toggle="modal"
                                   data-target="#myModalShow"><?= $list[$i]['name'] ?></a></td>
                            <td><?= $list[$i]['category_name'] ?></td>
                            <td><?php if ($list[$i]["is_shown"] == 1) {
                                    echo '<span class="label label-success">显示</span>';
                                } else {
                                    echo '<span class="label label-danger">不显示</span>';
                                } ?></td>
                            <td><?= $list[$i]['create_datetime'] ?></td>
                            <td>
                                <div class="btn-group">
                                    <button class="btn btn-link btn-xs" type="button"
                                            onclick="editData('<?= $list[$i]['id'] ?>','<?= $list[$i]['institution_code'] ?>')"
                                            data-toggle="modal"
                                            data-target="#myModalEdit">
                                        修改
                                    </button>
                                    <button class="btn btn-link btn-xs" type="button"
                                            data-toggle="modal"
                                            data-target="#deleteModal"
                                            onclick="delClick('<?= $list[$i]['id'] ?>')">
                                        删除
                                    </button>
                                </div>
                            </td>
                        </tr>
                        <?php
                    }
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<!-- Modal ad Edit-->
<div class="modal fade" id="myModalEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">友情链接（新增/修改）</h4>
                <h6 class="modal-title">机构：<span id="myModalEditLabelInst"></span></h6>
            </div>
            <div id="modaledit" class="modal-body">

            </div>
            <div class="modal-footer">
                <div class="btn-group">
                    <button type="button" id="continueAdd" class="btn btn-primary" onclick="continueAddData()"><i
                            class="fa fa-repeat"></i> 继续新增
                    </button>
                </div>
                <button type="button" class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消</button>
            </div>
        </div>
    </div>
</div>

<!-- Modal ad show-->
<div class="modal fade" id="myModalShow" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">友情链接（查看）</h4>
                <h6 class="modal-title">机构：<span id="myModalShowLabelInst"></span></h6>
            </div>
            <div id="modalshow" class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 关闭</button>
            </div>
        </div>
    </div>
</div>

<!--删除确认对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="deleteModal" class="modal fade">
    <div class="modal-dialog modal-sms">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">是否确认操作？</h4>
                <input type="hidden" id="deleteDataId" class="form-control" value="">
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" onclick="delData()"><i class="fa fa-ok"></i> 确认</button>
                <button class="btn btn-primary" onclick="delClose()"><i class="fa fa-remove"></i> 取消</button>
            </div>
        </div>
    </div>
</div>

<!--提示对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="hintMessageModal" class="modal fade">
    <div class="modal-dialog modal-sms">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body" id="hint_message" align="center">
            </div>
        </div>
    </div>
</div>
